Passed
Pull Request — master (#216)
by
unknown
02:09 queued 19s
created

GetContactByIdQueryHandler.execute   A

Complexity

Conditions 2

Size

Total Lines 16
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
dl 0
loc 16
rs 9.65
c 0
b 0
f 0
1
import { QueryHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { GetContactByIdQuery } from './GetContactByIdQuery';
4
import { IContactRepository } from 'src/Domain/Contact/Repository/IContactRepository';
5
import { ContactView } from '../View/ContactView';
6
import { ContactNotFoundException } from 'src/Domain/Contact/Exception/ContactNotFoundException';
7
8
@QueryHandler(GetContactByIdQuery)
9
export class GetContactByIdQueryHandler {
10
  constructor(
11
    @Inject('IContactRepository')
12
    private readonly contactRepository: IContactRepository
13
  ) {}
14
15
  public async execute(query: GetContactByIdQuery): Promise<ContactView> {
16
    const contact = await this.contactRepository.findOneById(query.id);
17
18
    if (!contact) {
19
      throw new ContactNotFoundException();
20
    }
21
22
    return new ContactView(
23
      contact.getId(),
24
      contact.getFirstName(),
25
      contact.getLastName(),
26
      contact.getCompany(),
27
      contact.getEmail(),
28
      contact.getPhoneNumber(),
29
      contact.getNotes()
30
    );
31
  }
32
}
33